package com.ubxtech.core.generator.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ubxtech.core.generator.IdGenerator;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Victor.Xiao
 * @since 2022-01-07 17:25
 **/
@Slf4j
public class HuToolIdGenerator implements IdGenerator {

    private final Snowflake snowflake;

    public HuToolIdGenerator(long workerId, long dataCenterId) {
        this.snowflake = IdUtil.getSnowflake(workerId, dataCenterId);
    }
    public HuToolIdGenerator() {
        long centerId = IdUtil.getDataCenterId(31);
        long workId = IdUtil.getWorkerId(centerId, 31);
        //参照Snowflake构造函数
        log.info("automatically generate DataCenterId : {},workId : {}", centerId, workId);
        this.snowflake = new Snowflake(workId, centerId);
    }

    @Override
    public long getUid() {
        return snowflake.nextId();
    }
}
